﻿--[[
   @brief    ui logic for login ui
   @author   rickonzhang, harleyxwang
   @version  1.0
   @date     2015-03-07
--]]

main_menue_control =
{
    exit_app_dlg_wnd_id = 0;
    
    init = function()
        base_utility.log("main_menue_control.init called");
        ierd_tgp.tpf_ui.Require("main_menue\\view.lua");
        ierd_tgp.tpf_ui.Require("main_menue\\model.lua");
        comm_center.reg_service("i_service_check_update", main_menue_control.check_update);
        comm_center.reg_service("i_service_exit_client", main_menue_control.will_exit_client_svr);
    end;
    
    open_main_menue = function(event_id, json_str)
        ierd_tgp.tpf_ui.Log("main_menue_control recve login_event.LOGIN_OPEN_MENUE");
        local info = base_utility.json_to_table(json_str);
        if nil == info then
            return;
        end
        main_menue_view.show(info.menu_id, info.is_show, info.offset_x, info.offset_y);
    end;
    
    switch_user = function()
        comm_center.call_service("i_svr_save_main_wnd_pos",{});
        --local gamelib =
      base_utility.get_component_adapter("IGame_library",
          "ierd_tgp.game_library.Get_game_library_adapter();");
        --gamelib:do_switch_user();
        main_menue_control.will_switch_user();
    end;
    
    switch_offline_mode = function(dwEventId, json_str)
        event_center.send_event(main_web_event.TGP_CLOSE, "{}");
        local param = base_utility.json_to_table(json_str);
        main_menue_control.will_switch_offline_mode();
    end;

    exit_tgp = function()
        comm_center.call_service("i_svr_save_main_wnd_pos",{});
        --local gamelib =
      base_utility.get_component_adapter("IGame_library",
          "ierd_tgp.game_library.Get_game_library_adapter();");
        --gamelib:do_exit_app();
        main_menue_control.will_exit_app();
    end;

    do_recommend_update = function()
        local gamelib =
      base_utility.get_component_adapter("IGame_library",
          "ierd_tgp.game_library.Get_game_library_adapter();");
        gamelib:do_update_app();
    end;
    
    --升级检查完成
    on_preupdate_detected = function(dwEventId, json_str)
        local param_table = base_utility.json_to_table(json_str);
        if param_table and param_table["has_recommend_update"] == true then
            main_menue_view.do_recommend_update();
            base_utility.log("有推荐升级");
        end;
    end;

    -- 调用game_lib 退出进程时候，修改为调用这个方法，来判断当前是否有游戏正在运行
    will_exit_app = function()
        local game_lib =
            base_utility.get_component_adapter("IGame_library",
                "ierd_tgp.game_library.Get_game_library_adapter();");
        local rail_host =
            base_utility.get_component_adapter("IRailHost",
                "ierd_tgp.tgp_plugin.GetRailHostAdapter();");
        local profile_is_syncing = rail_host:IsProfileSyncing();
        local is_game_installing = comm_center.call_service("Svr_IsGameInstalling", {});
        local exit_action = comm_center.call_service("Svr_GetCloseActionEx", {});
        local webgame_browser_running = main_menue_model.is_webgame_browser_running();
        local has_game_is_running_data = comm_center.call_service("SvrGetHasGameRunning", {});
        local has_game_is_running = false;
        if nil ~= has_game_is_running_data and has_game_is_running_data.has_game_running == 1 
            or game_lib:AnyCloudGameRunning() then
            has_game_is_running = true;
        end
        local has_game_downloading = false; -- download or update
        if nil ~= exit_action and exit_action.action == 1 then
            has_game_downloading = true;
        end

        if true == is_game_installing.is_game_installing or
           true == has_game_is_running or
           webgame_browser_running or
           profile_is_syncing or
           true == has_game_downloading
           then
            main_menue_control.handle_show_tips(main_menue_model.EXIT_TYPE_NOR); 
        else
            local show_propmt = false;
            local cfg_tbl = nil;
            local data = {key = "client_exit_config"};
            local cfg = comm_center.call_service("i_svr_get_common_cfg", data);
            if nil ~= cfg and nil ~= cfg.value and "" ~= cfg.value then
                cfg_tbl = base_utility.json_to_table(cfg.value);
                if nil ~= cfg_tbl then
                    show_propmt = cfg_tbl.show_prompt or false;
                end
            end

            if base_utility.is_type_of_oversea_ver() then
                show_propmt = false;
            end

            if show_propmt then
                main_menue_control.show_exit_client_popup(cfg_tbl);
            else
                event_center.send_event(main_web_event.TGP_CLOSE, "{}");
                event_center.send_event(group_event.TGP_CLOSE, "{}");
                local gamelib =
                    base_utility.get_component_adapter("IGame_library",
                        "ierd_tgp.game_library.Get_game_library_adapter();");
                layout_mgr.hide_all_window();
                comm_center.call_service("Svr_KillInstallProc", {});
                gamelib:do_exit_app();
            end
        end;
    end;


    -- 切换用户的时候，判断当前是否有游戏正在运行
    will_switch_user = function()
        local game_lib =
            base_utility.get_component_adapter("IGame_library",
                "ierd_tgp.game_library.Get_game_library_adapter();");
        local rail_host =
            base_utility.get_component_adapter("IRailHost",
                "ierd_tgp.tgp_plugin.GetRailHostAdapter();");
        local profile_is_syncing = rail_host:IsProfileSyncing();
        
        local webgame_browser_running = main_menue_model.is_webgame_browser_running();
        local is_game_installing = comm_center.call_service("Svr_IsGameInstalling", {});

        if true == is_game_installing.is_game_installing or
           banner_control.has_game_is_running() == 1 or
           webgame_browser_running or
           profile_is_syncing or game_lib:AnyCloudGameRunning()
           then
           main_menue_control.handle_show_tips(main_menue_model.EXIT_TYPE_SWITCH_USER);
        else 
            event_center.send_event(main_web_event.TGP_CLOSE, "{}");
            comm_center.call_service("Svr_KillInstallProc", {});
            local gamelib =
                base_utility.get_component_adapter("IGame_library",
                    "ierd_tgp.game_library.Get_game_library_adapter();");
            gamelib:do_switch_user();
        end;
    end;

    will_switch_offline_mode = function()
        local login_mgr = base_utility.get_component_adapter("ILogin_mgr",
                "ierd_tgp.login_mgr.GetLoginMgr();");
        login_mgr:set_offline_launch_flag(true);

        local game_lib = base_utility.get_component_adapter("IGame_library",
            "ierd_tgp.game_library.Get_game_library_adapter();");
        game_lib:do_switch_user();
    end;

    on_webgame_browser_game_start = function(eid, param)
        if param == nil or param.game_id == nil then
            return;
        end;
        main_menue_model.webgame_browser_running_games[param.game_id] = 1;
    end;

    on_sync_profile_end = function(event_id, json_str)
        base_utility.log("[main_menue_control]on_sync_profile_end, json_str: " .. json_str);
        local enabled = true;
        if main_menue_control.exit_app_dlg_wnd_id ~= 0 then
            poptips_control.enable_ok_btn(main_menue_control.exit_app_dlg_wnd_id, enabled, true);
        end
    end;
    
    on_webgame_browser_game_end = function(eid, param)
        if param == nil or param.game_id == nil then
            return;
        end;
        main_menue_model.webgame_browser_running_games[param.game_id] = nil;
    end;

    check_update = function (data)
        main_menue_view.on_click_update_item(nil);
    end;

    handle_show_tips = function (exit_type)
        local game_lib =
            base_utility.get_component_adapter("IGame_library",
                "ierd_tgp.game_library.Get_game_library_adapter();");
        if nil == game_lib then
            return;
        end

        local handle_fuction = main_menue_control.handle_exit_app;
        if main_menue_model.EXIT_TYPE_SWITCH_USER == exit_type then
            handle_fuction = main_menue_control.handle_switch_user;
        end
        local is_game_installing = comm_center.call_service("Svr_IsGameInstalling", {});
        local exit_action = comm_center.call_service("Svr_GetCloseActionEx", {});
        local has_game_downloading = false; -- download or update
        if nil ~= exit_action and exit_action.action == 1 then
            has_game_downloading = true;
        end
        -- 弹出对话框，提示用户
        local ok_btn_enabled = not profile_is_syncing;
        local tip_desc_title = "";
        local tip_desc = LoadStr("EXIT_ACC_DESC");
        if profile_is_syncing then
            tip_desc = LoadStr("EXIT_PROFILE_SYNC_DESC");
        elseif true == is_game_installing.is_game_installing then
            tip_desc = LoadStr("EXIT_GAME_INSTALLING_DESC");
        elseif game_lib:AnyAppStoreGameRunning() or game_lib:AnyCloudGameRunning() then
            tip_desc = LoadStr("EXIT_GAME_RUNNING_DESC");
        elseif true == has_game_downloading then
            tip_desc_title = LoadStr("EXIT_GAME_ACTIVE_DESC_TITLE");
            tip_desc = LoadStr("EXIT_GAME_ACTIVE_DESC");
        end;
            
        local cancel_txt = LoadStr("EXIT_TIP_RETURN");
        local ok_txt = LoadStr("EXIT_TIP_EXIT");
        if webgame_browser_running then
            tip_desc = LoadStr("EXIT_WEBGAME_RUNNING_DESC");
            cancel_txt = LoadStr("EXIT_BUTTON_CANCEL");
            ok_txt = LoadStr("EXIT_BUTTON_QUIT");
        end;

        if true == has_game_downloading then
            cancel_txt = LoadStr("EXIT_TIP_RETURN");
            ok_txt = LoadStr("EXIT_TIP_EXIT_V2");
        end

        local param = {
            ignore_bp_ui = true,
            position = 'center',--参数值: rbottom-右下角，center-居中
            title = LoadStr("EXIT_WND_TIP"),
            width = 480,
            height = 230,
            titleIcon = "",
            --参数值: ONEIMAGE:login\\base\\2\\login_bg.jpg
            icon = "",
            tipsIcon = "",
            descTitle = tip_desc_title,
            desc = tip_desc,
            cancelBtn = {
                visible = true,
                text = cancel_txt,
                handle = function()
                end
            },
            okBtn = {
                visible = true;
                text = ok_txt,
                handle = handle_fuction;
            }
        }
        main_menue_control.exit_app_dlg_wnd_id = poptips_control.show_tips(param);
        poptips_control.enable_ok_btn(main_menue_control.exit_app_dlg_wnd_id, ok_btn_enabled, false);
    end;

--    handle_has_game_is_installing = function (exit_type)
--        local game_lib =
--            base_utility.get_component_adapter("IGame_library",
--                "ierd_tgp.game_library.Get_game_library_adapter();");
--        if nil == game_lib then
--            return;
--        end

--        local handle_fuction = main_menue_control.handle_exit_app;
--        if main_menue_model.EXIT_TYPE_SWITCH_USER == exit_type then
--            handle_fuction = main_menue_control.handle_switch_user;
--        end
--        -- 弹出对话框，提示用户
--        local ok_btn_enabled = not profile_is_syncing;
--        local tip_desc = "游戏正在运行，退出WeGame将无法为您提供稳定的加速服务。";
--        if profile_is_syncing then
--            tip_desc = "正在同步存档文件，请稍候。";
--        elseif game_lib:AnyAppStoreGameRunning() then
--            tip_desc = "游戏进程正在运行，退出WeGame将会立即结束游戏进程。（立即退出游戏进程可能会影响您的游戏记录）";
--        end;

--        local cancel_txt = "返回";
--        local ok_txt = "强制退出";
--        if webgame_browser_running then
--            tip_desc = "WeGame退出时，页游也会一起关闭哦";
--            cancel_txt = "继续玩";
--            ok_txt = "狠心退出";
--        end;

--        local param = {
--            position = 'center',--参数值: rbottom-右下角，center-居中
--            title = "提示",
--            width = 480,
--            height = 230,
--            titleIcon = " ONEIMAGE:main\\main_acc.png",
--            --参数值: ONEIMAGE:login\\base\\2\\login_bg.jpg
--            icon = "",
--            tipsIcon = "",
--            descTitle = "",
--            desc = tip_desc,
--            cancelBtn = {
--                visible = true,
--                text = cancel_txt,
--                handle = function()
--                end
--            },
--            okBtn = {
--                visible = true;
--                text = ok_txt,
--                handle = handle_fuction;
--            }
--        }
--        main_menue_control.exit_app_dlg_wnd_id = poptips_control.show_tips(param);
--        poptips_control.enable_ok_btn(main_menue_control.exit_app_dlg_wnd_id, ok_btn_enabled, false);
--    end;

    handle_exit_app = function ()
        -- 退出前强杀安装进程
        comm_center.call_service("Svr_KillInstallProc", {});
        poptips_control.close_tips();
        event_center.send_event(main_web_event.TGP_CLOSE, "{}");
        event_center.send_event(group_event.TGP_CLOSE, "{}");
        layout_mgr.hide_all_window();
        local gamelib =
            base_utility.get_component_adapter("IGame_library",
                "ierd_tgp.game_library.Get_game_library_adapter();");
        if nil ~= gamelib then
            gamelib:do_exit_app();
        end  
    end;

    handle_switch_user = function ()
        -- 退出前强杀安装进程
        comm_center.call_service("Svr_KillInstallProc", {});
        poptips_control.close_tips();
        local gamelib =
            base_utility.get_component_adapter("IGame_library",
                "ierd_tgp.game_library.Get_game_library_adapter();");
        if nil ~= gamelib then
            gamelib:do_switch_user();
        end          
    end;

    show_exit_client_popup = function(config)
        local background_url = "";
        local jump_url = "";
        if nil ~= config then
            background_url = config.background_url or "";
            jump_url = config.jump_url or "";
        end

        local handle_fuction = main_menue_control.handle_exit_app;
        local param = {
            ui_file = "client_exit_popup.tpf",
            layout_file = "poptips\\client_exit_popup.lua",
            layout_callback = "client_exit_popup.on_preshow_callback",
            position = 'center',
            width = 480,
            height = 342,
            background = background_url,
            url = jump_url,
            cancelBtn = {},
            okBtn = {
                visible = true;
                text = "",
                handle = handle_fuction;
            }
        }
        poptips_control.show_tips(param);
    end;

    will_exit_client_svr = function(param)
        param = param or {};
        if not param.force_exit then
            main_menue_control.will_exit_app();
        else
            main_menue_control.handle_exit_app();
        end
    end;
};

main_menue_control.init();
